<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Register extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model("masterprofilealumni_model", "mpa");
        $this->load->model("city_model", "cmo");
        $this->load->model("usrprofile_model", "upm");
        $this->load->model("usraccount_model", "uam");
        
    }

    function index() {
        if ($this->input->post()) {
            $data["alumni"]->name = $this->input->post("txtName");
            $data["alumni"]->graduateyear = $this->input->post("ddAngkatan");
            $data["alumni"]->tglLahir = $this->input->post("tglLahir");

            $data["alumni"]->name == "" ? $data["err"]->name = strErr("mustfilled") : "";
            $tmpDate = split("-",$this->input->post("tglLahir"));
            $convDate = $tmpDate[2]."-".$tmpDate[1]."-".$tmpDate[0];
            $this->mpa->getAllCount("mpa_fullname = '".$data["alumni"]->name."' AND mpa_graduate_year = '". $data["alumni"]->graduateyear."' AND mpa_birth_date='".$convDate."'")== 0 ? $data["err"]->name = "Nama Anda tidak/belum terdaftar sebagai Alumni SMK Telkom Sandhy Putra Malang, jika Anda benar Alumni silahkan hubungi sekretariat melalui milis." : "";
            
            $data["alumni"]->graduateyear == "" ? $data["err"]->graduateyear = strErr("Alumni harus dipilih") : "";
            $data["alumni"]->tglLahir == "" ? $data["err"]->tglLahir = strErr("Tanggal Lahir harus disi") : "";
            
            if (@$data["err"]) {
                $this->myview(@$data, "register/index", true);
            } else {
                $row = $this->db->select('mpa_id_alumni, mpa_nis, mpa_fullname,mpa_birth_date')->from('master_profile_alumni')->where('mpa_fullname', $data["alumni"]->name)->get()->row();
                $profile = array (
                    'id_alumni' => $row->mpa_id_alumni,
                    'nis' => $row->mpa_nis,
                    'fullname' => $row->mpa_fullname,
                    'birth_date' => $this->input->post("tglLahir"),
                    'graduateyear' => $this->input->post("ddAngkatan")
                );
                $this->session->set_userdata($profile);
                
                redirect(base_url() . "register/inputdata");
            }
        } else {
            $this->myview(@$data, "register/index", true);
        }
    }

    function getKotaJSON(){
       $rsCity = $this->cmo->getByProp($this->input->get('pid'),''); 
        echo json_encode((array)$rsCity);
    }

    function inputdata(){
        if($this->input->post()){
            $data["err"] = null;
            
            $data["alumni"]->tempatlahir = $this->input->post("txtTempatLahir");
            if($data["alumni"]->tempatlahir == '')
                $data["err"]->tempatlahir = "Tempat Lahir harus di isi!" ;

            $data["alumni"]->status = $this->input->post("ddStatus");
            if($data["alumni"]->status == '')
                $data["err"]->status = "Status harus dipilih!";

            $data["alumni"]->email = $this->input->post("txtEmail");
            !validEmail($data["alumni"]->email) ? $data["err"]->email = "Email tidak benar" : "";

            $this->db->select("count(*) jum")->from("usr_account")->where("ua_email", $data["alumni"]->email)->get()->row()->jum > 0 ? $data["err"]->email = "Account Anda sudah terdaftar, gunakan <a href=\"" . base_url() . "alumni/fpass\">Lupa Password Anda?</a> untuk mereset password Anda" : "";

            $data["alumni"]->notelp = $this->input->post("txtNoTelp");
            
            $data["alumni"]->alamat = $this->input->post("txtAlamat");
            if($data['alumni']->alamat == '')
                $data['err']->alamat = "Alamat harus diisi!";
            
            $data["alumni"]->province_id = $this->input->post("ddPropinsi");
            if($data['alumni']->province_id == '')
                $data['err']->province_id = "Propinsi belum dipilih!";

            $data["alumni"]->city_id = $this->input->post("ddKota");
            if($data['alumni']->city_id == '')
                $data['err']->city_id = 'Kota / Kabupaten belum dipilih!';

            $data["alumni"]->kodepos = $this->input->post("txtKodePos");
            if($data['alumni']->kodepos == '')
                $data['err']->kodepos = 'Kode Pos harus diisi!';

            $dataTemp = array(
                'tempatlahir' => $this->input->post("txtTempatLahir"),
                'status' => $this->input->post("ddStatus"),
                'email' => $this->input->post("txtEmail"),
                'notelp' => $this->input->post("txtNoTelp"),
                'alamat' => $this->input->post("txtAlamat"),
                'province_id' => $this->input->post("ddPropinsi"),
                'kodepos' => $this->input->post("txtKodePos"),
                'city_id' => $this->input->post("ddKota")
            );

            $this->session->set_userdata($dataTemp);

            if (@$data["err"]) {
                $this->myview(@$data, "register/inputdata", true);
            } else {
                $rsMpa = $this->mpa->getById($this->session->userdata('id_alumni'));
                $password = randomPassword();
                
                $this->upm->insert($rsMpa->mpa_id_alumni, $rsMpa->mpa_nis, $rsMpa->mpa_fullname,
                    $this->session->userdata('tempatlahir'), $rsMpa->mpa_birth_date,
                    $rsMpa->mpa_gender, $rsMpa->mpa_graduate_year, $this->session->userdata('notelp'),
                    $this->session->userdata('email'),  $this->session->userdata('alamat'),
                    $this->session->userdata('city_id'), $this->session->userdata('province_id'),
                    $this->session->userdata('kodepos'), $this->session->userdata('status'), '',
                    $rsMpa->mpa_father, $rsMpa->mpa_mother, $rsMpa->mpa_home_phone, $rsMpa->mpa_home_address,
                    $rsMpa->mpa_home_city, '');//$rsMpa->mpa_home_province

               
                $this->uam->insert($rsMpa->mpa_id_alumni, $this->session->userdata('email')
                    , md5($password), $this->session->userdata('email'), $this->session->userdata('notelp'),
                    null, null, null, null, 0);

                $kodeAktivasi = randomPassword();
                $this->session->set_userdata('kode_aktivasi',$kodeAktivasi);
                
                //send mail
                $config = Array(
                    'protocol' => 'smtp',
                    'smtp_host' => 'ssl://smtp.googlemail.com',
                    'smtp_port' => 465,
                    'smtp_user' => 'sekretariat@wikusama.com',
                    'smtp_pass' => 'widyaloka',
                    'mailtype'  => 'html',
                    'charset'   => 'iso-8859-1'

                );

                $this->load->library('email', $config);
                $this->email->set_newline("\r\n");
                $this->email->from('abumamail@gmail.com', 'Sekretariat WIKUSAMA');
                $this->email->subject('Salam IAW');
                
                $this->email->to($this->session->userdata('email'));
                $message = 'Salam IAW,<br/>
                <br/>
                Selamat, Anda sudah terdaftar di database Ikatan Alumni Wikusama http://alumni.wikusama.org<br/>
                ID Alumni Anda adalah <strong>'.$rsMpa->mpa_id_alumni.'</strong><br/>
                Selanjutnya, lakukan aktivasi dengan mengunjungi http://alumni.wikusama.org/register/activation/ dan masukkan kode aktivasi berikut:
                <br/><br/>
                <div style="background-color:yellow;" align="center"><br/>'.$kodeAktivasi.'<br/><br/></div>
                <br/>
                Atau klik link aktivasi  http://alumni.wikusama.org/register/activation/'.$kodeAktivasi.'
                <br/><br/>
                Silahkan login dengan username: <strong>' . $this->session->userdata('email') . '</strong> dan password: <strong>' . $password . '</strong>.<br/>
                <br/>
                Perbarui data Anda dan menangkan hadiah menarik dari IAW setiap bulannya.<br/>
                <br/>
                Ditunggu peran sertanya.<br/>
                <br/>
                Terima kasih.';
                $this->email->message($message);
                if ($this->email->send()) {
                    $this->myview(@$data, "register/activation", true);
                } else {
                    $data["err"]->sendmail = "Pengiriman email gagal, mohon untuk menghubungi admin kami segera. Terima kasih.";
                    $this->myview(@$data, "register/inputdata", true);
                }
            
            }
        }else {
            $this->myview(@$data, "register/inputdata", true);
        }
    }

    function activation(){
        if($this->input->post()){
            if($this->session->userdata('kode_aktivasi') == $this->input->post("txtKodeAktivasi")){
                $data = array ('mpa_member_status'=>'A');
                $this->mpa->update($this->session->userdata('id_alumni'), $data, '');
                $this->session->sess_destroy();
                $this->myview(@$data, "register/info", true);
            } else {
                $data["err"]->aktivasi = "Kode yang anda masukkan salah, silahkan cek kode aktivasi anda.";
                $this->myview(@$data, "register/activation", true);
            }
        } else {
            $data['code'] = $this->uri->segment(3);
            $this->myview(@$data, "register/activation", true);
        }
    }
	
	function sendpass() {
        $config = Array(
            'protocol' => 'smtp',
            'smtp_host' => 'ssl://smtp.googlemail.com',
            'smtp_port' => 465,
            'smtp_user' => 'sekretariat@wikusama.org',
            'smtp_pass' => 'widyaloka'
        );

        $this->load->library('email', $config);
        $this->email->set_newline("\r\n");
        $this->email->from('sekretariat@wikusama.com', 'Sekretariat WIKUSAMA');
        $this->email->set_mailtype("html");
        $this->email->subject('Salam IAW');
        $dt = $this->db->limit(50, 0)->get("que_pass")->result();
        foreach ($dt as $k => $v) {
            $this->email->to($v->email);
            $message = 'Salam IAW,<br/>
            <br/>
            Selamat, Anda sudah terdaftar di database Ikatan Alumni Wikusama http://alumni.wikusama.org<br/>
            Selanjutnya, silakan mengupdate info Anda di web tersebut dengan username: ' . $v->email . ' dan password: ' . $v->password . '.<br/>
            <br/>
            Perbarui datanya dan menangkan hadiah menarik dari IAW setiap bulannya.<br/>
            <br/>
            Ditunggu peran sertanya.<br/>
            <br/>
            Terima kasih.';
            $this->email->message($message);
            if ($this->email->send()) {
                $this->db->where("email", $v->email)->delete("que_pass");
            }
        }
    }

}